import Mock from 'mockjs'
import api from './api'
import { getContent } from './content'
const mocks = [...api]
// 设置一下模拟返回数据时间
Mock.setup({
  timeout: '200-300'
})
/**
 * 将url改写成正则模式，mock匹配get请求路由时候使用，因为get请求参数是拼接在url后面，不用正则匹配不到
 * @param {String} path
 * @returns {RegExp}
 */
function regUrl(path: any): RegExp {
  const url = import.meta.env.VITE_APP_BASE_URL + path
  return RegExp(url + '.*')
}

function mockXHR() {
  for (let i of mocks) {
    Mock.mock(regUrl(i.url), i.type || 'get', (res) => {
      const index = getContent(res)
      // console.log(index,i.resData[index].response)
      return Mock.mock(i.resData[index].response)
    })
  }
}

export { mockXHR }
